home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / utility-src / fileutils / src / ls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-30  |  43.6 KB  |  1,955 lines  |  [TEXT/EMAC]

  1. /* `dir', `vdir' and `ls' directory listing programs for GNU.
  2.    Copyright (C) 1985, 1988, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* If the macro MULTI_COL is defined,
  19.    the multi-column format is the default regardless
  20.    of the type of output device.
  21.    This is for the `dir' program.
  22.  
  23.    If the macro LONG_FORMAT is defined,
  24.    the long format is the default regardless of the
  25.    type of output device.
  26.    This is for the `vdir' program.
  27.  
  28.    If neither is defined,
  29.    the output format depends on whether the output
  30.    device is a terminal.
  31.    This is for the `ls' program. */
  32.  
  33. /* Written by Richard Stallman and David MacKenzie. */
  34.  
  35. #ifdef _AIX
  36.  #pragma alloca
  37. #endif
  38.  
  39. #ifdef HAVE_CONFIG_H
  40. #if defined (CONFIG_BROKETS)
  41. /* We use <config.h> instead of "config.h" so that a compilation
  42.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  43.    (which it would do because it found this file in $srcdir).  */
  44. #include <config.h>
  45. #else
  46. #include "config.h"
  47. #endif
  48. #endif
  49.  
  50. #include "sys/types.h"
  51. #if !defined(_POSIX_SOURCE) || defined(_AIX)
  52. #include "sys/ioctl.h"
  53. #endif
  54. #include "stdio.h"
  55. #include "grp.h"
  56. #include "pwd.h"
  57. #include "sys/dir.h"
  58. #include "getopt.h"
  59. #include "system.h"
  60. #include "fnmatch.h"
  61.  
  62. #include "ls.h"
  63. #include "version.h"
  64.  
  65. #ifndef S_IEXEC
  66. #define S_IEXEC S_IXUSR
  67. #endif
  68.  
  69. /* Return an int indicating the result of comparing two longs. */
  70. #ifdef INT_16_BITS
  71. #define longdiff(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
  72. #else
  73. #define longdiff(a, b) ((a) - (b))
  74. #endif
  75.  
  76. #ifndef STDC_HEADERS
  77. char *ctime ();
  78. time_t time ();
  79. #endif
  80.  
  81. void mode_string ();
  82.  
  83. char *xstrdup ();
  84. char *getgroup ();
  85. char *getuser ();
  86. char *xmalloc ();
  87. char *xrealloc ();
  88. int argmatch ();
  89. void error ();
  90. void invalid_arg ();
  91.  
  92. static char *make_link_path ();
  93. static int compare_atime ();
  94. static int rev_cmp_atime ();
  95. static int compare_ctime ();
  96. static int rev_cmp_ctime ();
  97. static int compare_mtime ();
  98. static int rev_cmp_mtime ();
  99. static int compare_size ();
  100. static int rev_cmp_size ();
  101. static int compare_name ();
  102. static int rev_cmp_name ();
  103. static int compare_extension ();
  104. static int rev_cmp_extension ();
  105. static int decode_switches ();
  106. static int file_interesting ();
  107. static int gobble_file ();
  108. static int is_not_dot_or_dotdot ();
  109. static int length_of_file_name_and_frills ();
  110. static void add_ignore_pattern ();
  111. static void attach ();
  112. static void clear_files ();
  113. static void extract_dirs_from_files ();
  114. static void get_link_name ();
  115. static void indent ();
  116. static void print_current_files ();
  117. static void print_dir ();
  118. static void print_file_name_and_frills ();
  119. static void print_horizontal ();
  120. static void print_long_format ();
  121. static void print_many_per_line ();
  122. static void print_name_with_quoting ();
  123. static void print_type_indicator ();
  124. static void print_with_commas ();
  125. static void queue_directory (char *,char *);
  126. static void sort_files ();
  127. static void usage ();
  128.  
  129.  
  130. /* The name the program was run with, stripped of any leading path. */
  131. char *program_name;
  132.  
  133. enum filetype
  134. {
  135.   symbolic_link,
  136.   directory,
  137.   arg_directory,        /* Directory given as command line arg. */
  138.   normal1            /* All others. */
  139. };
  140.  
  141. struct file
  142. {
  143.   /* The file name. */
  144.   char *name;
  145.  
  146.   struct stat stat;
  147.  
  148.   /* For symbolic link, name of the file linked to, otherwise zero. */
  149.   char *linkname;
  150.  
  151.   /* For symbolic link and long listing, st_mode of file linked to, otherwise
  152.      zero. */
  153.   unsigned int linkmode;
  154.  
  155.   enum filetype filetype;
  156. };
  157.  
  158. /* The table of files in the current directory:
  159.  
  160.    `files' points to a vector of `struct file', one per file.
  161.    `nfiles' is the number of elements space has been allocated for.
  162.    `files_index' is the number actually in use.  */
  163.  
  164. /* Address of block containing the files that are described.  */
  165.  
  166. static struct file *files;
  167.  
  168. /* Length of block that `files' points to, measured in files.  */
  169.  
  170. static int nfiles;
  171.  
  172. /* Index of first unused in `files'.  */
  173.  
  174. static int files_index;
  175.  
  176. /* Record of one pending directory waiting to be listed.  */
  177.  
  178. struct pending
  179. {
  180.   char *name;
  181.   /* If the directory is actually the file pointed to by a symbolic link we
  182.      were told to list, `realname' will contain the name of the symbolic
  183.      link, otherwise zero. */
  184.   char *realname;
  185.   struct pending *next;
  186. };
  187.  
  188. static struct pending *pending_dirs;
  189.  
  190. /* Current time (seconds since 1970).  When we are printing a file's time,
  191.    include the year if it is more than 6 months before this time.  */
  192.  
  193. static time_t current_time;
  194.  
  195. /* The number of digits to use for block sizes.
  196.    4, or more if needed for bigger numbers.  */
  197.  
  198. static int block_size_size;
  199.  
  200. /* Option flags */
  201.  
  202. /* long_format for lots of info, one per line.
  203.    one_per_line for just names, one per line.
  204.    many_per_line for just names, many per line, sorted vertically.
  205.    horizontal for just names, many per line, sorted horizontally.
  206.    with_commas for just names, many per line, separated by commas.
  207.  
  208.    -l, -1, -C, -x and -m control this parameter.  */
  209.  
  210. enum format
  211. {
  212.   long_format,            /* -l */
  213.   one_per_line,            /* -1 */
  214.   many_per_line,        /* -C */
  215.   horizontal,            /* -x */
  216.   with_commas            /* -m */
  217. };
  218.  
  219. static enum format format;
  220.  
  221. /* Type of time to print or sort by.  Controlled by -c and -u.  */
  222.  
  223. enum time_type
  224. {
  225.   time_mtime,            /* default */
  226.   time_ctime,            /* -c */
  227.   time_atime            /* -u */
  228. };
  229.  
  230. static enum time_type time_type;
  231.  
  232. /* print the full time, otherwise the standard unix heuristics. */
  233.  
  234. int full_time;
  235.  
  236. /* The file characteristic to sort by.  Controlled by -t, -S, -U, -X. */
  237.  
  238. enum sort_type
  239. {
  240.   sort_none,            /* -U */
  241.   sort_name,            /* default */
  242.   sort_extension,        /* -X */
  243.   sort_time,            /* -t */
  244.   sort_size            /* -S */
  245. };
  246.  
  247. static enum sort_type sort_type;
  248.  
  249. /* Direction of sort.
  250.    0 means highest first if numeric,
  251.    lowest first if alphabetic;
  252.    these are the defaults.
  253.    1 means the opposite order in each case.  -r  */
  254.  
  255. static int sort_reverse;
  256.  
  257. /* Nonzero means to NOT display group information.  -G  */
  258.  
  259. int inhibit_group;
  260.  
  261. /* Nonzero means print the user and group id's as numbers rather
  262.    than as names.  -n  */
  263.  
  264. static int numeric_users;
  265.  
  266. /* Nonzero means mention the size in 512 byte blocks of each file.  -s  */
  267.  
  268. static int print_block_size;
  269.  
  270. /* Nonzero means show file sizes in kilobytes instead of blocks
  271.    (the size of which is system-dependent).  -k */
  272.  
  273. static int kilobyte_blocks;
  274.  
  275. /* none means don't mention the type of files.
  276.    all means mention the types of all files.
  277.    not_programs means do so except for executables.
  278.  
  279.    Controlled by -F and -p.  */
  280.  
  281. enum indicator_style
  282. {
  283.   none,                /* default */
  284.   all,                /* -F */
  285.   not_programs            /* -p */
  286. };
  287.  
  288. static enum indicator_style indicator_style;
  289.  
  290. /* Nonzero means mention the inode number of each file.  -i  */
  291.  
  292. static int print_inode;
  293.  
  294. /* Nonzero means when a symbolic link is found, display info on
  295.    the file linked to.  -L  */
  296.  
  297. static int trace_links;
  298.  
  299. /* Nonzero means when a directory is found, display info on its
  300.    contents.  -R  */
  301.  
  302. static int trace_dirs;
  303.  
  304. /* Nonzero means when an argument is a directory name, display info
  305.    on it itself.  -d  */
  306.  
  307. static int immediate_dirs;
  308.  
  309. /* Nonzero means don't omit files whose names start with `.'.  -A */
  310.  
  311. static int all_files;
  312.  
  313. /* Nonzero means don't omit files `.' and `..'
  314.    This flag implies `all_files'.  -a  */
  315.  
  316. static int really_all_files;
  317.  
  318. /* A linked list of shell-style globbing patterns.  If a non-argument
  319.    file name matches any of these patterns, it is omitted.
  320.    Controlled by -I.  Multiple -I options accumulate.
  321.    The -B option adds `*~' and `.*~' to this list.  */
  322.  
  323. struct ignore_pattern
  324. {
  325.   char *pattern;
  326.   struct ignore_pattern *next;
  327. };
  328.  
  329. static struct ignore_pattern *ignore_patterns;
  330.  
  331. /* Nonzero means quote nongraphic chars in file names.  -b  */
  332.  
  333. static int quote_funny_chars;
  334.  
  335. /* Nonzero means output nongraphic chars in file names as `?'.  -q  */
  336.  
  337. static int qmark_funny_chars;
  338.  
  339. /* Nonzero means output each file name using C syntax for a string.
  340.    Always accompanied by `quote_funny_chars'.
  341.    This mode, together with -x or -C or -m,
  342.    and without such frills as -F or -s,
  343.    is guaranteed to make it possible for a program receiving
  344.    the output to tell exactly what file names are present.  -Q  */
  345.  
  346. static int quote_as_string;
  347.  
  348. /* The number of chars per hardware tab stop.  -T */
  349. static int tabsize;
  350.  
  351. /* Nonzero means we are listing the working directory because no
  352.    non-option arguments were given. */
  353.  
  354. static int dir_defaulted;
  355.  
  356. /* Nonzero means print each directory name before listing it. */
  357.  
  358. static int print_dir_name;
  359.  
  360. /* The line length to use for breaking lines in many-per-line format.
  361.    Can be set with -w.  */
  362.  
  363. static int line_length;
  364.  
  365. /* If nonzero, the file listing format requires that stat be called on
  366.    each file. */
  367.  
  368. static int format_needs_stat;
  369.  
  370. /* The exit status to use if we don't get any fatal errors. */
  371.  
  372. static int exit_status;
  373.  
  374. /* If non-zero, display usage information and exit.  */
  375. static int show_help;
  376.  
  377. /* If non-zero, print the version on standard output and exit.  */
  378. static int show_version;
  379.  
  380. static struct option long_options[] =
  381. {
  382.   {"all", no_argument, 0, 'a'},
  383.   {"escape", no_argument, 0, 'b'},
  384.   {"directory", no_argument, 0, 'd'},
  385.   {"full-time", no_argument, 0, 1},
  386.   {"inode", no_argument, 0, 'i'},
  387.   {"kilobytes", no_argument, 0, 'k'},
  388.   {"numeric-uid-gid", no_argument, 0, 'n'},
  389.   {"no-group", no_argument, 0, 'G'},
  390.   {"hide-control-chars", no_argument, 0, 'q'},
  391.   {"reverse", no_argument, 0, 'r'},
  392.   {"size", no_argument, 0, 's'},
  393.   {"width", required_argument, 0, 'w'},
  394.   {"almost-all", no_argument, 0, 'A'},
  395.   {"ignore-backups", no_argument, 0, 'B'},
  396.   {"classify", no_argument, 0, 'F'},
  397.   {"file-type", no_argument, 0, 'F'},
  398.   {"ignore", required_argument, 0, 'I'},
  399.   {"dereference", no_argument, 0, 'L'},
  400.   {"literal", no_argument, 0, 'N'},
  401.   {"quote-name", no_argument, 0, 'Q'},
  402.   {"recursive", no_argument, 0, 'R'},
  403.   {"format", required_argument, 0, 12},
  404.   {"sort", required_argument, 0, 10},
  405.   {"tabsize", required_argument, 0, 'T'},
  406.   {"time", required_argument, 0, 11},
  407.   {"help", no_argument, 0, 1},
  408.   {"version", no_argument, 0, 1},
  409.   {0, 0, 0, 0}
  410. };
  411.  
  412. static char format_args[][15] =
  413. {
  414.   "verbose", "long", "commas", "horizontal", "across",
  415.   "vertical", "single-column", 0
  416. };
  417.  
  418. static enum format const formats[] =
  419. {
  420.   long_format, long_format, with_commas, horizontal, horizontal,
  421.   many_per_line, one_per_line
  422. };
  423.  
  424. static char sort_args[][10] =
  425. {
  426.   "none", "time", "size", "extension", 0
  427. };
  428.  
  429. static enum sort_type const sort_types[] =
  430. {
  431.   sort_none, sort_time, sort_size, sort_extension
  432. };
  433.  
  434. static char time_args[][10] =
  435. {
  436.   "atime", "access", "use", "ctime", "status", 0
  437. };
  438.  
  439. static enum time_type const time_types[] =
  440. {
  441.   time_atime, time_atime, time_atime, time_ctime, time_ctime
  442. };
  443.  
  444.  
  445. void
  446. main (argc, argv)
  447.      int argc;
  448.      char **argv;
  449. {
  450.   register int i;
  451.   register struct pending *thispend;
  452.  
  453.     for (i = 0; i<sizeof(long_options) / sizeof(struct option); ++i) {
  454.         if (!strcmp(long_options[i].name,"full-time"))
  455.             long_options[i].flag = &full_time;
  456.         if (!strcmp(long_options[i].name,"help"))
  457.             long_options[i].flag = &show_help;
  458.         if (!strcmp(long_options[i].name,"version"))
  459.             long_options[i].flag = &show_version;
  460.     }
  461.  
  462.  
  463.   exit_status = 0;
  464.   dir_defaulted = 1;
  465.   print_dir_name = 1;
  466.   pending_dirs = 0;
  467.   current_time = time ((time_t *) 0);
  468.  
  469.   program_name = argv[0];
  470.   i = decode_switches (argc, argv);
  471.  
  472.   if (show_version)
  473.     {
  474.       printf ("%s\n", version_string);
  475.       exit (0);
  476.     }
  477.  
  478.   if (show_help)
  479.     usage (0);
  480.  
  481.   format_needs_stat = sort_type == sort_time || sort_type == sort_size
  482.     || format == long_format
  483.     || trace_links || trace_dirs || indicator_style != none
  484.     || print_block_size || print_inode;
  485.  
  486.   nfiles = 100;
  487.   files = (struct file *) xmalloc (sizeof (struct file) * nfiles);
  488.   files_index = 0;
  489.  
  490.   clear_files ();
  491.  
  492.   if (i < argc)
  493.     dir_defaulted = 0;
  494.   for (; i < argc; i++)
  495.     gobble_file (argv[i], 1, "");
  496.  
  497.   if (dir_defaulted)
  498.     {
  499.       if (immediate_dirs)
  500.     gobble_file (".", 1, "");
  501.       else
  502.     queue_directory (".", 0);
  503.     }
  504.  
  505.   if (files_index)
  506.     {
  507.       sort_files ();
  508.       if (!immediate_dirs)
  509.     extract_dirs_from_files ("", 0);
  510.       /* `files_index' might be zero now.  */
  511.     }
  512.   if (files_index)
  513.     {
  514.       print_current_files ();
  515.       if (pending_dirs)
  516.     putchar ('\n');
  517.     }
  518.   else if (pending_dirs && pending_dirs->next == 0)
  519.     print_dir_name = 0;
  520.  
  521.   while (pending_dirs)
  522.     {
  523.       thispend = pending_dirs;
  524.       pending_dirs = pending_dirs->next;
  525.       print_dir (thispend->name, thispend->realname);
  526.       free (thispend->name);
  527.       if (thispend->realname)
  528.     free (thispend->realname);
  529.       free (thispend);
  530.       print_dir_name = 1;
  531.     }
  532.  
  533. #ifdef STAND_ALONE
  534.   fflush(stdout);
  535. #endif
  536.   exit (exit_status);
  537. }
  538.  
  539. /* Set all the option flags according to the switches specified.
  540.    Return the index of the first non-option argument.  */
  541.  
  542. static int
  543. decode_switches (argc, argv)
  544.      int argc;
  545.      char **argv;
  546. {
  547.   register char *p;
  548.   int c;
  549.   int i;
  550.  
  551.   qmark_funny_chars = 0;
  552.   quote_funny_chars = 0;
  553.  
  554.   /* initialize all switches to default settings */
  555.  
  556.   switch (ls_mode)
  557.     {
  558.     case LS_MULTI_COL:
  559.       /* This is for the `dir' program.  */
  560.       format = many_per_line;
  561.       quote_funny_chars = 1;
  562.       break;
  563.  
  564.     case LS_LONG_FORMAT:
  565.       /* This is for the `vdir' program.  */
  566.       format = long_format;
  567.       quote_funny_chars = 1;
  568.       break;
  569.  
  570.     case LS_LS:
  571.       /* This is for the `ls' program.  */
  572.       if (isatty (1))
  573.     {
  574.       format = many_per_line;
  575.       qmark_funny_chars = 1;
  576.     }
  577.       else
  578.     {
  579.       format = one_per_line;
  580.       qmark_funny_chars = 0;
  581.     }
  582.       break;
  583.  
  584.     default:
  585.       abort ();
  586.     }
  587.  
  588.   time_type = time_mtime;
  589.   full_time = 0;
  590.   sort_type = sort_name;
  591.   sort_reverse = 0;
  592.   numeric_users = 0;
  593.   print_block_size = 0;
  594.   kilobyte_blocks = getenv ("POSIXLY_CORRECT") == 0;
  595.   indicator_style = none;
  596.   print_inode = 0;
  597.   trace_links = 0;
  598.   trace_dirs = 0;
  599.   immediate_dirs = 0;
  600.   all_files = 0;
  601.   really_all_files = 0;
  602.   ignore_patterns = 0;
  603.   quote_as_string = 0;
  604.  
  605.   p = getenv ("COLUMNS");
  606.   line_length = p ? atoi (p) : 80;
  607.  
  608. #ifdef TIOCGWINSZ
  609.   {
  610.     struct winsize ws;
  611.  
  612.     if (ioctl (1, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
  613.       line_length = ws.ws_col;
  614.   }
  615. #endif
  616.  
  617.   p = getenv ("TABSIZE");
  618.   tabsize = p ? atoi (p) : 8;
  619.  
  620.   while ((c = getopt_long (argc, argv, "abcdfgiklmnpqrstuw:xABCFGI:LNQRST:UX1",
  621.                long_options, (int *) 0)) != EOF)
  622.     {
  623.       switch (c)
  624.     {
  625.     case 0:
  626.       break;
  627.  
  628.     case 'a':
  629.       all_files = 1;
  630.       really_all_files = 1;
  631.       break;
  632.  
  633.     case 'b':
  634.       quote_funny_chars = 1;
  635.       qmark_funny_chars = 0;
  636.       break;
  637.  
  638.     case 'c':
  639.       time_type = time_ctime;
  640.       break;
  641.  
  642.     case 'd':
  643.       immediate_dirs = 1;
  644.       break;
  645.  
  646.     case 'f':
  647.       /* Same as enabling -a -U and disabling -l -s.  */
  648.       all_files = 1;
  649.       really_all_files = 1;
  650.       sort_type = sort_none;
  651.       /* disable -l */
  652.       if (format == long_format)
  653.         format = (isatty (1) ? many_per_line : one_per_line);
  654.       print_block_size = 0;  /* disable -s */
  655.       break;
  656.  
  657.     case 'g':
  658.       /* No effect.  For BSD compatibility. */
  659.       break;
  660.  
  661.     case 'i':
  662.       print_inode = 1;
  663.       break;
  664.  
  665.     case 'k':
  666.       kilobyte_blocks = 1;
  667.       break;
  668.  
  669.     case 'l':
  670.       format = long_format;
  671.       break;
  672.  
  673.     case 'm':
  674.       format = with_commas;
  675.       break;
  676.  
  677.     case 'n':
  678.       numeric_users = 1;
  679.       break;
  680.  
  681.     case 'p':
  682.       indicator_style = not_programs;
  683.       break;
  684.  
  685.     case 'q':
  686.       qmark_funny_chars = 1;
  687.       quote_funny_chars = 0;
  688.       break;
  689.  
  690.     case 'r':
  691.       sort_reverse = 1;
  692.       break;
  693.  
  694.     case 's':
  695.       print_block_size = 1;
  696.       break;
  697.  
  698.     case 't':
  699.       sort_type = sort_time;
  700.       break;
  701.  
  702.     case 'u':
  703.       time_type = time_atime;
  704.       break;
  705.  
  706.     case 'w':
  707.       line_length = atoi (optarg);
  708.       if (line_length < 1)
  709.         error (1, 0, "invalid line width: %s", optarg);
  710.       break;
  711.  
  712.     case 'x':
  713.       format = horizontal;
  714.       break;
  715.  
  716.     case 'A':
  717.       all_files = 1;
  718.       break;
  719.  
  720.     case 'B':
  721.       add_ignore_pattern ("*~");
  722.       add_ignore_pattern (".*~");
  723.       break;
  724.  
  725.     case 'C':
  726.       format = many_per_line;
  727.       break;
  728.  
  729.     case 'F':
  730.       indicator_style = all;
  731.       break;
  732.  
  733.     case 'G':        /* inhibit display of group info */
  734.       inhibit_group = 1;
  735.       break;
  736.  
  737.     case 'I':
  738.       add_ignore_pattern (optarg);
  739.       break;
  740.  
  741.     case 'L':
  742.       trace_links = 1;
  743.       break;
  744.  
  745.     case 'N':
  746.       quote_funny_chars = 0;
  747.       qmark_funny_chars = 0;
  748.       break;
  749.  
  750.     case 'Q':
  751.       quote_as_string = 1;
  752.       quote_funny_chars = 1;
  753.       qmark_funny_chars = 0;
  754.       break;
  755.  
  756.     case 'R':
  757.       trace_dirs = 1;
  758.       break;
  759.  
  760.     case 'S':
  761.       sort_type = sort_size;
  762.       break;
  763.  
  764.     case 'T':
  765.       tabsize = atoi (optarg);
  766.       if (tabsize < 1)
  767.         error (1, 0, "invalid tab size: %s", optarg);
  768.       break;
  769.  
  770.     case 'U':
  771.       sort_type = sort_none;
  772.       break;
  773.  
  774.     case 'X':
  775.       sort_type = sort_extension;
  776.       break;
  777.  
  778.     case '1':
  779.       format = one_per_line;
  780.       break;
  781.  
  782.     case 10:        /* +sort */
  783.       i = argmatch (optarg, sort_args);
  784.       if (i < 0)
  785.         {
  786.           invalid_arg ("sort type", optarg, i);
  787.           usage (1);
  788.         }
  789.       sort_type = sort_types[i];
  790.       break;
  791.  
  792.     case 11:        /* +time */
  793.       i = argmatch (optarg, time_args);
  794.       if (i < 0)
  795.         {
  796.           invalid_arg ("time type", optarg, i);
  797.           usage (1);
  798.         }
  799.       time_type = time_types[i];
  800.       break;
  801.  
  802.     case 12:        /* +format */
  803.       i = argmatch (optarg, format_args);
  804.       if (i < 0)
  805.         {
  806.           invalid_arg ("format type", optarg, i);
  807.           usage (1);
  808.         }
  809.       format = formats[i];
  810.       break;
  811.  
  812.     default:
  813.       usage (1);
  814.     }
  815.     }
  816.  
  817.   return optind;
  818. }
  819.  
  820. /* Request that the directory named `name' have its contents listed later.
  821.    If `realname' is nonzero, it will be used instead of `name' when the
  822.    directory name is printed.  This allows symbolic links to directories
  823.    to be treated as regular directories but still be listed under their
  824.    real names. */
  825.  
  826. static void
  827. queue_directory (name, realname)
  828.      char *name;
  829.      char *realname;
  830. {
  831.   struct pending *new;
  832.  
  833.   new = (struct pending *) xmalloc (sizeof (struct pending));
  834.   new->next = pending_dirs;
  835.   pending_dirs = new;
  836.   new->name = xstrdup (name);
  837.   if (realname)
  838.     new->realname = xstrdup (realname);
  839.   else
  840.     new->realname = 0;
  841. }
  842.  
  843. /* Read directory `name', and list the files in it.
  844.    If `realname' is nonzero, print its name instead of `name';
  845.    this is used for symbolic links to directories. */
  846.  
  847. static void
  848. print_dir (name, realname)
  849.      char *name;
  850.      char *realname;
  851. {
  852.   register DIR *reading;
  853.   register struct dirent *next;
  854.   register int total_blocks = 0;
  855.  
  856.   errno = 0;
  857.   reading = opendir (name);
  858.   if (!reading)
  859.     {
  860.       error (0, errno, "%s", name);
  861.       exit_status = 1;
  862.       return;
  863.     }
  864.  
  865.   /* Read the directory entries, and insert the subfiles into the `files'
  866.      table.  */
  867.  
  868.   clear_files ();
  869.  
  870.   while ((next = readdir (reading)) != NULL)
  871.     if (file_interesting (next))
  872.       total_blocks += gobble_file (next->d_name, 0, name);
  873.  
  874.   if (CLOSEDIR (reading))
  875.     {
  876.       error (0, errno, "%s", name);
  877.       exit_status = 1;
  878.       /* Don't return; print whatever we got. */
  879.     }
  880.  
  881.   /* Sort the directory contents.  */
  882.   sort_files ();
  883.  
  884.   /* If any member files are subdirectories, perhaps they should have their
  885.      contents listed rather than being mentioned here as files.  */
  886.  
  887.   if (trace_dirs)
  888.     extract_dirs_from_files (name, 1);
  889.  
  890.   if (print_dir_name)
  891.     {
  892.       if (realname)
  893.     printf ("%s:\n", realname);
  894.       else
  895.     printf ("%s:\n", name);
  896.     }
  897.  
  898.   if (format == long_format || print_block_size)
  899.     printf ("total %u\n", total_blocks);
  900.  
  901.   if (files_index)
  902.     print_current_files ();
  903.  
  904.   if (pending_dirs)
  905.     putchar ('\n');
  906. }
  907.  
  908. /* Add `pattern' to the list of patterns for which files that match are
  909.    not listed.  */
  910.  
  911. static void
  912. add_ignore_pattern (pattern)
  913.      char *pattern;
  914. {
  915.   register struct ignore_pattern *ignore;
  916.  
  917.   ignore = (struct ignore_pattern *) xmalloc (sizeof (struct ignore_pattern));
  918.   ignore->pattern = pattern;
  919.   /* Add it to the head of the linked list. */
  920.   ignore->next = ignore_patterns;
  921.   ignore_patterns = ignore;
  922. }
  923.  
  924. /* Return nonzero if the file in `next' should be listed. */
  925.  
  926. static int
  927. file_interesting (next)
  928.      register struct dirent *next;
  929. {
  930.   register struct ignore_pattern *ignore;
  931.  
  932.   for (ignore = ignore_patterns; ignore; ignore = ignore->next)
  933.     if (fnmatch (ignore->pattern, next->d_name, FNM_PERIOD) == 0)
  934.       return 0;
  935.  
  936.   if (really_all_files
  937.       || next->d_name[0] != '.'
  938.       || (all_files
  939.       && next->d_name[1] != '\0'
  940.       && (next->d_name[1] != '.' || next->d_name[2] != '\0')))
  941.     return 1;
  942.  
  943.   return 0;
  944. }
  945.  
  946. /* Enter and remove entries in the table `files'.  */
  947.  
  948. /* Empty the table of files. */
  949.  
  950. static void
  951. clear_files ()
  952. {
  953.   register int i;
  954.  
  955.   for (i = 0; i < files_index; i++)
  956.     {
  957.       free (files[i].name);
  958.       if (files[i].linkname)
  959.     free (files[i].linkname);
  960.     }
  961.  
  962.   files_index = 0;
  963.   block_size_size = 4;
  964. }
  965.  
  966. /* Add a file to the current table of files.
  967.    Verify that the file exists, and print an error message if it does not.
  968.    Return the number of blocks that the file occupies.  */
  969.  
  970. static int
  971. gobble_file (name, explicit_arg, dirname)
  972.      char *name;
  973.      int explicit_arg;
  974.      char *dirname;
  975. {
  976.   register int blocks;
  977.   register int val;
  978.   register char *path;
  979.  
  980.   if (files_index == nfiles)
  981.     {
  982.       nfiles *= 2;
  983.       files = (struct file *) xrealloc (files, sizeof (struct file) * nfiles);
  984.     }
  985.  
  986.   files[files_index].linkname = 0;
  987.   files[files_index].linkmode = 0;
  988.  
  989.   if (explicit_arg || format_needs_stat)
  990.     {
  991.       /* `path' is the absolute pathname of this file. */
  992.  
  993.       if (name[0] == '/' || dirname[0] == 0)
  994.     path = name;
  995.       else
  996.     {
  997.       path = (char *) alloca (strlen (name) + strlen (dirname) + 2);
  998.       attach (path, dirname, name);
  999.     }
  1000.  
  1001.       if (trace_links)
  1002.     {
  1003.       val = stat (path, &files[files_index].stat);
  1004.       if (val < 0)
  1005.         /* Perhaps a symbolically-linked to file doesn't exist; stat
  1006.            the link instead. */
  1007.         val = lstat (path, &files[files_index].stat);
  1008.     }
  1009.       else
  1010.     val = lstat (path, &files[files_index].stat);
  1011.       if (val < 0)
  1012.     {
  1013.       error (0, errno, "%s", path);
  1014.       exit_status = 1;
  1015.       return 0;
  1016.     }
  1017.  
  1018. #ifdef S_ISLNK
  1019.       if (S_ISLNK (files[files_index].stat.st_mode)
  1020.       && (explicit_arg || format == long_format))
  1021.     {
  1022.       char *linkpath;
  1023.       struct stat linkstats;
  1024.  
  1025.       get_link_name (path, &files[files_index]);
  1026.       linkpath = make_link_path (path, files[files_index].linkname);
  1027.  
  1028.       /* Avoid following symbolic links when possible, ie, when
  1029.          they won't be traced and when no indicator is needed. */
  1030.       if (linkpath
  1031.           && ((explicit_arg && format != long_format)
  1032.            || indicator_style != none)
  1033.           && stat (linkpath, &linkstats) == 0)
  1034.         {
  1035.           /* Symbolic links to directories that are mentioned on the
  1036.          command line are automatically traced if not being
  1037.          listed as files.  */
  1038.           if (explicit_arg && format != long_format
  1039.           && S_ISDIR (linkstats.st_mode))
  1040.         {
  1041.           /* Substitute the linked-to directory's name, but
  1042.              save the real name in `linkname' for printing.  */
  1043.           if (!immediate_dirs)
  1044.             {
  1045.               char *tempname = name;
  1046.               name = linkpath;
  1047.               linkpath = files[files_index].linkname;
  1048.               files[files_index].linkname = tempname;
  1049.             }
  1050.           files[files_index].stat = linkstats;
  1051.         }
  1052.           else
  1053.         /* Get the linked-to file's mode for the filetype indicator
  1054.            in long listings.  */
  1055.         files[files_index].linkmode = linkstats.st_mode;
  1056.         }
  1057.       if (linkpath)
  1058.         free (linkpath);
  1059.     }
  1060. #endif
  1061.  
  1062. #ifdef S_ISLNK
  1063.       if (S_ISLNK (files[files_index].stat.st_mode))
  1064.     files[files_index].filetype = symbolic_link;
  1065.       else
  1066. #endif
  1067.     if (S_ISDIR (files[files_index].stat.st_mode))
  1068.       {
  1069.         if (explicit_arg && !immediate_dirs)
  1070.           files[files_index].filetype = arg_directory;
  1071.         else
  1072.           files[files_index].filetype = directory;
  1073.       }
  1074.     else
  1075.       files[files_index].filetype = normal1;
  1076.  
  1077.       blocks = convert_blocks (ST_NBLOCKS (files[files_index].stat),
  1078.                    kilobyte_blocks);
  1079.       if (blocks >= 10000 && block_size_size < 5)
  1080.     block_size_size = 5;
  1081.       if (blocks >= 100000 && block_size_size < 6)
  1082.     block_size_size = 6;
  1083.       if (blocks >= 1000000 && block_size_size < 7)
  1084.     block_size_size = 7;
  1085.     }
  1086.   else
  1087.     blocks = 0;
  1088.  
  1089.   files[files_index].name = xstrdup (name);
  1090.   files_index++;
  1091.  
  1092.   return blocks;
  1093. }
  1094.  
  1095. #ifdef S_ISLNK
  1096.  
  1097. /* Put the name of the file that `filename' is a symbolic link to
  1098.    into the `linkname' field of `f'. */
  1099.  
  1100. static void
  1101. get_link_name (filename, f)
  1102.      char *filename;
  1103.      struct file *f;
  1104. {
  1105.   char *linkbuf;
  1106.   register int linksize;
  1107.  
  1108.   linkbuf = (char *) alloca (PATH_MAX + 2);
  1109.   /* Some automounters give incorrect st_size for mount points.
  1110.      I can't think of a good workaround for it, though.  */
  1111.   linksize = readlink (filename, linkbuf, PATH_MAX + 1);
  1112.   if (linksize < 0)
  1113.     {
  1114.       error (0, errno, "%s", filename);
  1115.       exit_status = 1;
  1116.     }
  1117.   else
  1118.     {
  1119.       linkbuf[linksize] = '\0';
  1120.       f->linkname = xstrdup (linkbuf);
  1121.     }
  1122. }
  1123.  
  1124. /* If `linkname' is a relative path and `path' contains one or more
  1125.    leading directories, return `linkname' with those directories
  1126.    prepended; otherwise, return a copy of `linkname'.
  1127.    If `linkname' is zero, return zero. */
  1128.  
  1129. static char *
  1130. make_link_path (path, linkname)
  1131.      char *path;
  1132.      char *linkname;
  1133. {
  1134.   char *linkbuf;
  1135.   int bufsiz;
  1136.  
  1137.   if (linkname == 0)
  1138.     return 0;
  1139.  
  1140.   if (*linkname == '/')
  1141.     return xstrdup (linkname);
  1142.  
  1143.   /* The link is to a relative path.  Prepend any leading path
  1144.      in `path' to the link name. */
  1145.   linkbuf = rindex (path, '/');
  1146.   if (linkbuf == 0)
  1147.     return xstrdup (linkname);
  1148.  
  1149.   bufsiz = linkbuf - path + 1;
  1150.   linkbuf = xmalloc (bufsiz + strlen (linkname) + 1);
  1151.   strncpy (linkbuf, path, bufsiz);
  1152.   strcpy (linkbuf + bufsiz, linkname);
  1153.   return linkbuf;
  1154. }
  1155. #endif
  1156.  
  1157. /* Remove any entries from `files' that are for directories,
  1158.    and queue them to be listed as directories instead.
  1159.    `dirname' is the prefix to prepend to each dirname
  1160.    to make it correct relative to ls's working dir.
  1161.    `recursive' is nonzero if we should not treat `.' and `..' as dirs.
  1162.    This is desirable when processing directories recursively.  */
  1163.  
  1164. static void
  1165. extract_dirs_from_files (dirname, recursive)
  1166.      char *dirname;
  1167.      int recursive;
  1168. {
  1169.   register int i, j;
  1170.   register char *path;
  1171.   int dirlen;
  1172.  
  1173.   dirlen = strlen (dirname) + 2;
  1174.   /* Queue the directories last one first, because queueing reverses the
  1175.      order.  */
  1176.   for (i = files_index - 1; i >= 0; i--)
  1177.     if ((files[i].filetype == directory || files[i].filetype == arg_directory)
  1178.     && (!recursive || is_not_dot_or_dotdot (files[i].name)))
  1179.       {
  1180.     if (files[i].name[0] == '/' || dirname[0] == 0)
  1181.       {
  1182.         queue_directory (files[i].name, files[i].linkname);
  1183.       }
  1184.     else
  1185.       {
  1186.         path = (char *) xmalloc (strlen (files[i].name) + dirlen);
  1187.         attach (path, dirname, files[i].name);
  1188.         queue_directory (path, files[i].linkname);
  1189.         free (path);
  1190.       }
  1191.     if (files[i].filetype == arg_directory)
  1192.       free (files[i].name);
  1193.       }
  1194.  
  1195.   /* Now delete the directories from the table, compacting all the remaining
  1196.      entries.  */
  1197.  
  1198.   for (i = 0, j = 0; i < files_index; i++)
  1199.     if (files[i].filetype != arg_directory)
  1200.       files[j++] = files[i];
  1201.   files_index = j;
  1202. }
  1203.  
  1204. /* Return non-zero if `name' doesn't end in `.' or `..'
  1205.    This is so we don't try to recurse on `././././. ...' */
  1206.  
  1207. static int
  1208. is_not_dot_or_dotdot (name)
  1209.      char *name;
  1210. {
  1211.   char *t;
  1212.  
  1213.   t = rindex (name, '/');
  1214.   if (t)
  1215.     name = t + 1;
  1216.  
  1217.   if (name[0] == '.'
  1218.       && (name[1] == '\0'
  1219.       || (name[1] == '.' && name[2] == '\0')))
  1220.     return 0;
  1221.  
  1222.   return 1;
  1223. }
  1224.  
  1225. /* Sort the files now in the table.  */
  1226.  
  1227. static void
  1228. sort_files ()
  1229. {
  1230.   int (*func) ();
  1231.  
  1232.   switch (sort_type)
  1233.     {
  1234.     case sort_none:
  1235.       return;
  1236.     case sort_time:
  1237.       switch (time_type)
  1238.     {
  1239.     case time_ctime:
  1240.       func = sort_reverse ? rev_cmp_ctime : compare_ctime;
  1241.       break;
  1242.     case time_mtime:
  1243.       func = sort_reverse ? rev_cmp_mtime : compare_mtime;
  1244.       break;
  1245.     case time_atime:
  1246.       func = sort_reverse ? rev_cmp_atime : compare_atime;
  1247.       break;
  1248.     default:
  1249.       abort ();
  1250.     }
  1251.       break;
  1252.     case sort_name:
  1253.       func = sort_reverse ? rev_cmp_name : compare_name;
  1254.       break;
  1255.     case sort_extension:
  1256.       func = sort_reverse ? rev_cmp_extension : compare_extension;
  1257.       break;
  1258.     case sort_size:
  1259.       func = sort_reverse ? rev_cmp_size : compare_size;
  1260.       break;
  1261.     default:
  1262.       abort ();
  1263.     }
  1264.  
  1265.   qsort (files, files_index, sizeof (struct file), func);
  1266. }
  1267.  
  1268. /* Comparison routines for sorting the files. */
  1269.  
  1270. static int
  1271. compare_ctime (file1, file2)
  1272.      struct file *file1, *file2;
  1273. {
  1274.   return longdiff (file2->stat.st_ctime, file1->stat.st_ctime);
  1275. }
  1276.  
  1277. static int
  1278. rev_cmp_ctime (file2, file1)
  1279.      struct file *file1, *file2;
  1280. {
  1281.   return longdiff (file2->stat.st_ctime, file1->stat.st_ctime);
  1282. }
  1283.  
  1284. static int
  1285. compare_mtime (file1, file2)
  1286.      struct file *file1, *file2;
  1287. {
  1288.   return longdiff (file2->stat.st_mtime, file1->stat.st_mtime);
  1289. }
  1290.  
  1291. static int
  1292. rev_cmp_mtime (file2, file1)
  1293.      struct file *file1, *file2;
  1294. {
  1295.   return longdiff (file2->stat.st_mtime, file1->stat.st_mtime);
  1296. }
  1297.  
  1298. static int
  1299. compare_atime (file1, file2)
  1300.      struct file *file1, *file2;
  1301. {
  1302.   return longdiff (file2->stat.st_atime, file1->stat.st_atime);
  1303. }
  1304.  
  1305. static int
  1306. rev_cmp_atime (file2, file1)
  1307.      struct file *file1, *file2;
  1308. {
  1309.   return longdiff (file2->stat.st_atime, file1->stat.st_atime);
  1310. }
  1311.  
  1312. static int
  1313. compare_size (file1, file2)
  1314.      struct file *file1, *file2;
  1315. {
  1316.   return longdiff (file2->stat.st_size, file1->stat.st_size);
  1317. }
  1318.  
  1319. static int
  1320. rev_cmp_size (file2, file1)
  1321.      struct file *file1, *file2;
  1322. {
  1323.   return longdiff (file2->stat.st_size, file1->stat.st_size);
  1324. }
  1325.  
  1326. static int
  1327. compare_name (file1, file2)
  1328.      struct file *file1, *file2;
  1329. {
  1330.   return strcmp (file1->name, file2->name);
  1331. }
  1332.  
  1333. static int
  1334. rev_cmp_name (file2, file1)
  1335.      struct file *file1, *file2;
  1336. {
  1337.   return strcmp (file1->name, file2->name);
  1338. }
  1339.  
  1340. /* Compare file extensions.  Files with no extension are `smallest'.
  1341.    If extensions are the same, compare by filenames instead. */
  1342.  
  1343. static int
  1344. compare_extension (file1, file2)
  1345.      struct file *file1, *file2;
  1346. {
  1347.   register char *base1, *base2;
  1348.   register int cmp;
  1349.  
  1350.   base1 = rindex (file1->name, '.');
  1351.   base2 = rindex (file2->name, '.');
  1352.   if (base1 == 0 && base2 == 0)
  1353.     return strcmp (file1->name, file2->name);
  1354.   if (base1 == 0)
  1355.     return -1;
  1356.   if (base2 == 0)
  1357.     return 1;
  1358.   cmp = strcmp (base1, base2);
  1359.   if (cmp == 0)
  1360.     return strcmp (file1->name, file2->name);
  1361.   return cmp;
  1362. }
  1363.  
  1364. static int
  1365. rev_cmp_extension (file2, file1)
  1366.      struct file *file1, *file2;
  1367. {
  1368.   register char *base1, *base2;
  1369.   register int cmp;
  1370.  
  1371.   base1 = rindex (file1->name, '.');
  1372.   base2 = rindex (file2->name, '.');
  1373.   if (base1 == 0 && base2 == 0)
  1374.     return strcmp (file1->name, file2->name);
  1375.   if (base1 == 0)
  1376.     return -1;
  1377.   if (base2 == 0)
  1378.     return 1;
  1379.   cmp = strcmp (base1, base2);
  1380.   if (cmp == 0)
  1381.     return strcmp (file1->name, file2->name);
  1382.   return cmp;
  1383. }
  1384.  
  1385. /* List all the files now in the table.  */
  1386.  
  1387. static void
  1388. print_current_files ()
  1389. {
  1390.   register int i;
  1391.  
  1392.   switch (format)
  1393.     {
  1394.     case one_per_line:
  1395.       for (i = 0; i < files_index; i++)
  1396.     {
  1397.       print_file_name_and_frills (files + i);
  1398.       putchar ('\n');
  1399.     }
  1400.       break;
  1401.  
  1402.     case many_per_line:
  1403.       print_many_per_line ();
  1404.       break;
  1405.  
  1406.     case horizontal:
  1407.       print_horizontal ();
  1408.       break;
  1409.  
  1410.     case with_commas:
  1411.       print_with_commas ();
  1412.       break;
  1413.  
  1414.     case long_format:
  1415.       for (i = 0; i < files_index; i++)
  1416.     {
  1417.       print_long_format (files + i);
  1418.       putchar ('\n');
  1419.     }
  1420.       break;
  1421.     }
  1422. }
  1423.  
  1424. static void
  1425. print_long_format (f)
  1426.      struct file *f;
  1427. {
  1428.   char modebuf[20];
  1429.   char timebuf[40];
  1430.   time_t when;
  1431.  
  1432.   mode_string (f->stat.st_mode, modebuf);
  1433.   modebuf[10] = 0;
  1434.  
  1435.   switch (time_type)
  1436.     {
  1437.     case time_ctime:
  1438.       when = f->stat.st_ctime;
  1439.       break;
  1440.     case time_mtime:
  1441.       when = f->stat.st_mtime;
  1442.       break;
  1443.     case time_atime:
  1444.       when = f->stat.st_atime;
  1445.       break;
  1446.     }
  1447.  
  1448.   strcpy (timebuf, ctime (&when));
  1449.  
  1450.   if (full_time)
  1451.     timebuf[24] = '\0';
  1452.   else
  1453.     {
  1454.       if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
  1455.       || current_time < when - 60L * 60L) /* In the future. */
  1456.     {
  1457.       /* The file is fairly old or in the future.
  1458.          POSIX says the cutoff is 6 months old;
  1459.          approximate this by 6*30 days.
  1460.          Allow a 1 hour slop factor for what is considered "the future",
  1461.          to allow for NFS server/client clock disagreement.
  1462.          Show the year instead of the time of day.  */
  1463.       strcpy (timebuf + 11, timebuf + 19);
  1464.     }
  1465.       timebuf[16] = 0;
  1466.     }
  1467.  
  1468.   if (print_inode)
  1469.     printf ("%6lu ", (unsigned long) f->stat.st_ino);
  1470.  
  1471.   if (print_block_size)
  1472.     printf ("%*u ", block_size_size,
  1473.         (unsigned) convert_blocks (ST_NBLOCKS (f->stat),
  1474.                         kilobyte_blocks));
  1475.  
  1476.   /* The space between the mode and the number of links is the POSIX
  1477.      "optional alternate access method flag". */
  1478.   printf ("%s %3u ", modebuf, f->stat.st_nlink);
  1479.  
  1480.   if (numeric_users)
  1481.     printf ("%-8u ", (unsigned int) f->stat.st_uid);
  1482.   else
  1483.     printf ("%-8.8s ", getuser (f->stat.st_uid));
  1484.  
  1485.   if (!inhibit_group)
  1486.     {
  1487.       if (numeric_users)
  1488.     printf ("%-8u ", (unsigned int) f->stat.st_gid);
  1489.       else
  1490.     printf ("%-8.8s ", getgroup (f->stat.st_gid));
  1491.     }
  1492.  
  1493.   if (S_ISCHR (f->stat.st_mode) || S_ISBLK (f->stat.st_mode))
  1494.     printf ("%3u, %3u ", (unsigned) major (f->stat.st_rdev),
  1495.         (unsigned) minor (f->stat.st_rdev));
  1496.   else
  1497.     printf ("%8lu ", f->stat.st_size);
  1498.  
  1499.   printf ("%s ", full_time ? timebuf : timebuf + 4);
  1500.  
  1501.   print_name_with_quoting (f->name);
  1502.  
  1503.   if (f->filetype == symbolic_link)
  1504.     {
  1505.       if (f->linkname)
  1506.     {
  1507.       fputs (" -> ", stdout);
  1508.       print_name_with_quoting (f->linkname);
  1509.       if (indicator_style != none)
  1510.         print_type_indicator (f->linkmode);
  1511.     }
  1512.     }
  1513.   else if (indicator_style != none)
  1514.     print_type_indicator (f->stat.st_mode);
  1515. }
  1516.  
  1517. static void
  1518. print_name_with_quoting (p)
  1519.      register char *p;
  1520. {
  1521.   register unsigned char c;
  1522.  
  1523.   if (quote_as_string)
  1524.     putchar ('"');
  1525.  
  1526.   while ((c = *p++))
  1527.     {
  1528.       if (quote_funny_chars)
  1529.     {
  1530.       switch (c)
  1531.         {
  1532.         case '\\':
  1533.           printf ("\\\\");
  1534.           break;
  1535.  
  1536.         case '\n':
  1537.           printf ("\\n");
  1538.           break;
  1539.  
  1540.         case '\b':
  1541.           printf ("\\b");
  1542.           break;
  1543.  
  1544.         case '\r':
  1545.           printf ("\\r");
  1546.           break;
  1547.  
  1548.         case '\t':
  1549.           printf ("\\t");
  1550.           break;
  1551.  
  1552.         case '\f':
  1553.           printf ("\\f");
  1554.           break;
  1555.  
  1556.         case ' ':
  1557.           printf ("\\ ");
  1558.           break;
  1559.  
  1560.         case '"':
  1561.           printf ("\\\"");
  1562.           break;
  1563.  
  1564.         default:
  1565.           if (c > 040 && c < 0177)
  1566.         putchar (c);
  1567.           else
  1568.         printf ("\\%03o", (unsigned int) c);
  1569.         }
  1570.     }
  1571.       else
  1572.     {
  1573.       if (c >= 040 && c < 0177)
  1574.         putchar (c);
  1575.       else if (!qmark_funny_chars)
  1576.         putchar (c);
  1577.       else
  1578.         putchar ('?');
  1579.     }
  1580.     }
  1581.  
  1582.   if (quote_as_string)
  1583.     putchar ('"');
  1584. }
  1585.  
  1586. /* Print the file name of `f' with appropriate quoting.
  1587.    Also print file size, inode number, and filetype indicator character,
  1588.    as requested by switches.  */
  1589.  
  1590. static void
  1591. print_file_name_and_frills (f)
  1592.      struct file *f;
  1593. {
  1594.   if (print_inode)
  1595.     printf ("%6lu ", (unsigned long) f->stat.st_ino);
  1596.  
  1597.   if (print_block_size)
  1598.     printf ("%*u ", block_size_size,
  1599.         (unsigned) convert_blocks (ST_NBLOCKS (f->stat),
  1600.                         kilobyte_blocks));
  1601.  
  1602.   print_name_with_quoting (f->name);
  1603.  
  1604.   if (indicator_style != none)
  1605.     print_type_indicator (f->stat.st_mode);
  1606. }
  1607.  
  1608. static void
  1609. print_type_indicator (mode)
  1610.      unsigned int mode;
  1611. {
  1612.   if (S_ISDIR (mode))
  1613.     putchar ('/');
  1614.  
  1615. #ifdef S_ISLNK
  1616.   if (S_ISLNK (mode))
  1617.     putchar ('@');
  1618. #endif
  1619.  
  1620. #ifdef S_ISFIFO
  1621.   if (S_ISFIFO (mode))
  1622.     putchar ('|');
  1623. #endif
  1624.  
  1625. #ifdef S_ISSOCK
  1626.   if (S_ISSOCK (mode))
  1627.     putchar ('=');
  1628. #endif
  1629.  
  1630.   if (S_ISREG (mode) && indicator_style == all
  1631.       && (mode & (S_IEXEC | S_IEXEC >> 3 | S_IEXEC >> 6)))
  1632.     putchar ('*');
  1633. }
  1634.  
  1635. static int
  1636. length_of_file_name_and_frills (f)
  1637.      struct file *f;
  1638. {
  1639.   register char *p = f->name;
  1640.   register char c;
  1641.   register int len = 0;
  1642.  
  1643.   if (print_inode)
  1644.     len += 7;
  1645.  
  1646.   if (print_block_size)
  1647.     len += 1 + block_size_size;
  1648.  
  1649.   if (quote_as_string)
  1650.     len += 2;
  1651.  
  1652.   while ((c = *p++))
  1653.     {
  1654.       if (quote_funny_chars)
  1655.     {
  1656.       switch (c)
  1657.         {
  1658.         case '\\':
  1659.         case '\n':
  1660.         case '\b':
  1661.         case '\r':
  1662.         case '\t':
  1663.         case '\f':
  1664.         case ' ':
  1665.           len += 2;
  1666.           break;
  1667.  
  1668.         case '"':
  1669.           if (quote_as_string)
  1670.         len += 2;
  1671.           else
  1672.         len += 1;
  1673.           break;
  1674.  
  1675.         default:
  1676.           if (c >= 040 && c < 0177)
  1677.         len += 1;
  1678.           else
  1679.         len += 4;
  1680.         }
  1681.     }
  1682.       else
  1683.     len += 1;
  1684.     }
  1685.  
  1686.   if (indicator_style != none)
  1687.     {
  1688.       unsigned filetype = f->stat.st_mode;
  1689.  
  1690.       if (S_ISREG (filetype))
  1691.     {
  1692.       if (indicator_style == all
  1693.           && (f->stat.st_mode & (S_IEXEC | S_IEXEC >> 3 | S_IEXEC >> 6)))
  1694.         len += 1;
  1695.     }
  1696.       else if (S_ISDIR (filetype)
  1697. #ifdef S_ISLNK
  1698.            || S_ISLNK (filetype)
  1699. #endif
  1700. #ifdef S_ISFIFO
  1701.            || S_ISFIFO (filetype)
  1702. #endif
  1703. #ifdef S_ISSOCK
  1704.            || S_ISSOCK (filetype)
  1705. #endif
  1706.            )
  1707.     len += 1;
  1708.     }
  1709.  
  1710.   return len;
  1711. }
  1712.  
  1713. static void
  1714. print_many_per_line ()
  1715. {
  1716.   int filesno;            /* Index into files. */
  1717.   int row;            /* Current row. */
  1718.   int max_name_length;        /* Length of longest file name + frills. */
  1719.   int name_length;        /* Length of each file name + frills. */
  1720.   int pos;            /* Current character column. */
  1721.   int cols;            /* Number of files across. */
  1722.   int rows;            /* Maximum number of files down. */
  1723.  
  1724.   /* Compute the maximum file name length.  */
  1725.   max_name_length = 0;
  1726.   for (filesno = 0; filesno < files_index; filesno++)
  1727.     {
  1728.       name_length = length_of_file_name_and_frills (files + filesno);
  1729.       if (name_length > max_name_length)
  1730.     max_name_length = name_length;
  1731.     }
  1732.  
  1733.   /* Allow at least two spaces between names.  */
  1734.   max_name_length += 2;
  1735.  
  1736.   /* Calculate the maximum number of columns that will fit. */
  1737.   cols = line_length / max_name_length;
  1738.   if (cols == 0)
  1739.     cols = 1;
  1740.   /* Calculate the number of rows that will be in each column except possibly
  1741.      for a short column on the right. */
  1742.   rows = files_index / cols + (files_index % cols != 0);
  1743.   /* Recalculate columns based on rows. */
  1744.   cols = files_index / rows + (files_index % rows != 0);
  1745.  
  1746.   for (row = 0; row < rows; row++)
  1747.     {
  1748.       filesno = row;
  1749.       pos = 0;
  1750.       /* Print the next row.  */
  1751.       while (1)
  1752.     {
  1753.       print_file_name_and_frills (files + filesno);
  1754.       name_length = length_of_file_name_and_frills (files + filesno);
  1755.  
  1756.       filesno += rows;
  1757.       if (filesno >= files_index)
  1758.         break;
  1759.  
  1760.       indent (pos + name_length, pos + max_name_length);
  1761.       pos += max_name_length;
  1762.     }
  1763.       putchar ('\n');
  1764.     }
  1765. }
  1766.  
  1767. static void
  1768. print_horizontal ()
  1769. {
  1770.   int filesno;
  1771.   int max_name_length;
  1772.   int name_length;
  1773.   int cols;
  1774.   int pos;
  1775.  
  1776.   /* Compute the maximum file name length.  */
  1777.   max_name_length = 0;
  1778.   for (filesno = 0; filesno < files_index; filesno++)
  1779.     {
  1780.       name_length = length_of_file_name_and_frills (files + filesno);
  1781.       if (name_length > max_name_length)
  1782.     max_name_length = name_length;
  1783.     }
  1784.  
  1785.   /* Allow two spaces between names.  */
  1786.   max_name_length += 2;
  1787.  
  1788.   cols = line_length / max_name_length;
  1789.   if (cols == 0)
  1790.     cols = 1;
  1791.  
  1792.   pos = 0;
  1793.   name_length = 0;
  1794.  
  1795.   for (filesno = 0; filesno < files_index; filesno++)
  1796.     {
  1797.       if (filesno != 0)
  1798.     {
  1799.       if (filesno % cols == 0)
  1800.         {
  1801.           putchar ('\n');
  1802.           pos = 0;
  1803.         }
  1804.       else
  1805.         {
  1806.           indent (pos + name_length, pos + max_name_length);
  1807.           pos += max_name_length;
  1808.         }
  1809.     }
  1810.  
  1811.       print_file_name_and_frills (files + filesno);
  1812.  
  1813.       name_length = length_of_file_name_and_frills (files + filesno);
  1814.     }
  1815.   putchar ('\n');
  1816. }
  1817.  
  1818. static void
  1819. print_with_commas ()
  1820. {
  1821.   int filesno;
  1822.   int pos, old_pos;
  1823.  
  1824.   pos = 0;
  1825.  
  1826.   for (filesno = 0; filesno < files_index; filesno++)
  1827.     {
  1828.       old_pos = pos;
  1829.  
  1830.       pos += length_of_file_name_and_frills (files + filesno);
  1831.       if (filesno + 1 < files_index)
  1832.     pos += 2;        /* For the comma and space */
  1833.  
  1834.       if (old_pos != 0 && pos >= line_length)
  1835.     {
  1836.       putchar ('\n');
  1837.       pos -= old_pos;
  1838.     }
  1839.  
  1840.       print_file_name_and_frills (files + filesno);
  1841.       if (filesno + 1 < files_index)
  1842.     {
  1843.       putchar (',');
  1844.       putchar (' ');
  1845.     }
  1846.     }
  1847.   putchar ('\n');
  1848. }
  1849.  
  1850. /* Assuming cursor is at position FROM, indent up to position TO.  */
  1851.  
  1852. static void
  1853. indent (from, to)
  1854.      int from, to;
  1855. {
  1856.   while (from < to)
  1857.     {
  1858.       if (to / tabsize > from / tabsize)
  1859.     {
  1860.       putchar ('\t');
  1861.       from += tabsize - from % tabsize;
  1862.     }
  1863.       else
  1864.     {
  1865.       putchar (' ');
  1866.       from++;
  1867.     }
  1868.     }
  1869. }
  1870.  
  1871. /* Put DIRNAME/NAME into DEST, handling `.' and `/' properly. */
  1872.  
  1873. static void
  1874. attach (dest, dirname, name)
  1875.      char *dest, *dirname, *name;
  1876. {
  1877.   char *dirnamep = dirname;
  1878.  
  1879.   /* Copy dirname if it is not ".". */
  1880.   if (dirname[0] != '.' || dirname[1] != 0)
  1881.     {
  1882.       while (*dirnamep)
  1883.     *dest++ = *dirnamep++;
  1884.       /* Add '/' if `dirname' doesn't already end with it. */
  1885.       if (dirnamep > dirname && dirnamep[-1] != '/')
  1886.     *dest++ = '/';
  1887.     }
  1888.   while (*name)
  1889.     *dest++ = *name++;
  1890.   *dest = 0;
  1891. }
  1892.  
  1893. static void
  1894. usage (status)
  1895.      int status;
  1896. {
  1897.   if (status != 0)
  1898.     fprintf (stderr, "Try `%s --help' for more information.\n",
  1899.          program_name);
  1900.   else
  1901.     {
  1902.       printf ("Usage: %s [OPTION]... [PATH]...\n", program_name);
  1903.       printf (
  1904. "\n"
  1905. "  -a, --all                  do not hide entries starting with .\n"
  1906. "  -b, --escape               print octal escapes for nongraphic characters\n"
  1907. "  -c                         sort by change time; with -l: show ctime\n"
  1908. "  -d, --directory            list directory entries instead of contents\n"
  1909. "  -f                         do not sort, enable -aU, disable -lst\n"
  1910. "  -g                         (ignored)\n"
  1911. "  -i, --inode                print index number of each file\n"
  1912. "  -k, --kilobytes            use 1024 blocks, not 512 despite POSIXLY_CORRECT\n"
  1913. "  -l                         use a long listing format\n"
  1914. "  -m                         fill width with a comma separated list of entries\n"
  1915. "  -n, --numeric-uid-gid      list numeric UIDs and GIDs instead of names\n"
  1916. "  -p                         append a character for typing each entry\n"
  1917. "  -q, --hide-control-chars   print ? instead of non graphic characters\n"
  1918. "  -r, --reverse              reverse order while sorting\n"
  1919. "  -s, --size                 print block size of each file\n"
  1920. "  -t                         sort by modification time; with -l: show mtime\n"
  1921. "  -u                         sort by last access time; with -l: show atime\n"
  1922. "  -w, --width COLS           assume screen width instead of current value\n"
  1923. "  -x                         list entries by lines instead of by columns\n"
  1924. "  -A, --almost-all           do not list implied . and ..\n");
  1925.  
  1926.       printf (
  1927. "  -B, --ignore-backups       do not list implied entries ending with ~\n"
  1928. "  -C                         list entries by columns\n"
  1929. "  -F, --classify             append a character for typing each entry\n"
  1930. "  -G, --no-group             inhibit display of group information\n"
  1931. "  -I, --ignore PATTERN       do not list implied entries matching shell PATTERN\n"
  1932. "  -L, --dereference          list entries pointed to by symbolic links\n"
  1933. "  -N, --literal              do not quote entry names\n"
  1934. "  -Q, --quote-name           enclose entry names in double quotes\n"
  1935. "  -R, --recursive            list subdirectories recursively\n"
  1936. "  -S                         sort by file size\n"
  1937. "  -T, --tabsize COLS         assume tab stops at each COLS instead of 8\n"
  1938. "  -U                         do not sort; list entries in directory order\n"
  1939. "  -X                         sort alphabetically by entry extension\n"
  1940. "  -1                         list one file per line\n"
  1941. "      --full-time            list both full date and full time\n"
  1942. "      --help                 display this help and exit\n"
  1943. "      --format WORD          across -x, commas -m, horizontal -x, long -l,\n"
  1944. "                               single-column -1, verbose -l, vertical -C\n"
  1945. "      --sort WORD            ctime -c, extension -X, none -U, size -S,\n"
  1946. "                               status -c, time -t\n"
  1947. "      --time WORD            atime -u, access -u, use -u\n"
  1948. "      --version              output version information and exit\n"
  1949. "\n"
  1950. "Sort entries alphabetically if none of -cftuSUX nor --sort.\n");
  1951.  
  1952.     }
  1953.   exit (status);
  1954. }
  1955.